/******************************************************************************
 * * ui_net_interface.h
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#ifndef _UI_NET_INTERFACE_H_
#define _UI_NET_INTERFACE_H_

#ifdef __cplusplus
extern "C" {
#endif /* __cpluscplus */

#ifdef LV_CONF_INCLUDE_SIMPLE
#include "lv_watch_conf.h"
#else
#include "../../../lv_watch_conf.h"
#endif

enum
{
    WATCH_QQ_OK = 0,
    WATCH_QQ_FAIL,
};                                           //for result

enum
{
    WATCH_QQ_REG_OK = 0,
    WATCH_QQ_SCANED_UNREG,
    WATCH_QQ_UNSCANED,
    WATCH_QQ_OTHER_FAIL,
};

typedef struct
{
    uint8_t  result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
    char *   string;                        //when result is 0,string is valid,string is the qrcode string code
} watch_qq_get_qr_cnf_t;

typedef struct
{
    uint8_t  result;                        //WATCH_QQ_REG_OK,WATCH_QQ_SCANED_UNREG,WATCH_QQ_UNSCANED,WATCH_QQ_OTHER_FAIL
    char     access_token[64];                //access_token,when result is WATCH_QUERY_QR_OK,it is valid
    char     openid[64];                    //openid,when result is WATCH_QUERY_QR_OK,it is valid
} watch_qq_query_qr_cnf_t;

typedef struct
{
    char     access_token[64];                //access_token
    char     openid[64];                    //openid
    uint8_t  state;                         //0 is online,1 is offline
} watch_qq_register_with_openid_req_t;

typedef struct
{
    uint8_t  result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
} watch_qq_register_with_openid_cnf_t;

typedef struct
{
    char    *cookie;                        //NULL should be used at the first time
    uint8_t    sync_flag;                        //0 means no messages to be got in last time,1 means there were some messages to be got in last time
    uint8_t    get_ramblemsg;                    //0 means no ramble messages to be got,1 means there were ramble messages for one day to be got
} watch_qq_get_msg_req_t;

enum {
    WATCH_QQ_MSG_TYPE_TEXT = 0,
    WATCH_QQ_MSG_TYPE_PIC,
    WATCH_QQ_MSG_TYPE_PTT,
    WATCH_QQ_MSG_TYPE_PTT_BIN,
    WATCH_QQ_MSG_TYPE_LOCATION,
    WATCH_QQ_MSG_TYPE_PIC_BIN,
    WATCH_QQ_MSG_TYPE_LOCATION_XML,
    WATCH_QQ_MSG_TYPE_FACE,
};

typedef struct
{
    /* download ptt */
    char     *url;        //url
    uint32_t duration;    //seconds
}watch_qq_ptt_msg_content_t;

typedef struct
{
    /* upload ptt data */
    char *pttbin;         //binary
    uint32_t length;
    uint32_t duration;    //seconds
}watch_qq_ptt_bin_msg_content_t;

typedef struct
{
    /* upload pic data */
    char *type;           //"jpeg","png","webp","pjpg","sharp","bmp","gif","apng"
    char *filename;
    uint32_t width;
    uint32_t height;
    char *picbin;         //binary
}watch_qq_pic_bin_msg_content_t;

typedef struct
{
    void    *msg_content;                   //message content, when get msg,the content is watch_qq_ptt_msg_content_t or text.when send msg,the content is watch_qq_pic_bin_msg_content_t or watch_qq_ptt_bin_msg_content_t.
    uint8_t msg_type;                       //message type,now support WATCH_QQ_MSG_TYPE_PIC,WATCH_QQ_MSG_TYPE_PTT,WATCH_QQ_MSG_TYPE_TEXT(for download),and WATCH_QQ_MSG_TYPE_PTT_BIN or WATCH_QQ_MSG_TYPE_PIC_BIN(for upload)
} watch_qq_msg_element_t;

typedef struct
{
    char         from_openid[64];            //the open id of the send side
    char         to_openid[64];                //the open id of the receive side
    uint32_t     msg_timestamp;
    uint64_t     msg_seqid;                 //msg sequence id,msgseqid = (msgseq << 32) | random,msgseq is the msg sequence number(16bit),random is the random number(32bit)
    uint8_t      msg_readed;                //1 means readed,0 means unreaded
    uint32_t     msg_elementcnt;
    lv_ll_t      msg_elements_ll;           //msg elements,watch_qq_msg_element_t
} watch_qq_msg_info_t;

typedef struct
{
    char         openid[64];                //the open id of user
    char         *headurl;                    //the url of head img
    char         *nickname;
    char         *remark;                   //only for get friend list
    uint32_t     contact_time;
    uint8_t      gender;                    //1 means male,0 means female
    char         qqid[16];                  //qqid
} watch_qq_user_info_t;

typedef struct
{
    uint8_t        result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
    char        *cookie;                    //new cookie for the user to get the msg next time
    uint8_t         sync_flag;                    //2 means no messages to be got this time,1 means there were some messages to be got this time
    uint32_t     msg_infocnt;
    lv_ll_t        msg_infos_ll;                //msg infos, watch_qq_msg_info_t
    uint32_t     user_infocnt;
    lv_ll_t      user_infos_ll;             //user infos,watch_qq_user_info_t
} watch_qq_get_msg_cnf_t;

typedef struct
{
    watch_qq_msg_info_t    msg_info;            //msg info,only support 1 msg send
    uint8_t         receiver_type;                //0 means the same type terminal can receive,1 means all types terminal can receive
} watch_qq_send_msg_req_t;

typedef struct
{
    uint16_t         apptype;                 //An independent app type assigned to each watch by Tencent, eg.asr is 120
} watch_qq_upload_statistics_req_t;

typedef struct
{
    uint8_t      result;                    //WATCH_QQ_OK,WATCH_QQ_FAIL
    uint32_t     server_time;               //the time of current server
    uint64_t     msg_seqid;
} watch_qq_send_msg_cnf_t;

typedef struct
{
    char        openid[64];                    //friend's openid
    uint32_t    lastmsg_time;                //the msg time of the last msg,0 should be used at the first time
    uint32_t    random;                     //the random number of the last msg,0 should be used at the first time
    uint32_t    get_msgcnt;                 //the count of need to get msgs
    uint8_t     direction;                  //0 means get msgs from old to new,1 means get msgs from new to old
} watch_qq_get_friend_msg_req_t;

typedef struct
{
    uint8_t        result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
    uint32_t    lastmsg_time;                //the msg time of the last msg,it should be used next time
    uint32_t    random;                     //the random number of the last msg,it should be used next time
    uint8_t     complete;                   //0 means not complete(get all msgs),1 means complete(get all msgs)
    uint32_t    msg_infocnt;
    lv_ll_t       msg_infos_ll;                //msg infos, watch_qq_msg_info_t
    uint32_t    user_infocnt;
    lv_ll_t     user_infos_ll;              //user infos,watch_qq_user_info_t
} watch_qq_get_friend_msg_cnf_t;

typedef struct
{
    uint32_t    update_seq;                    //the update sequence number of friend list,0 should be used at the first time
    uint32_t    start_friendindex;          //the start index of friend list,0 should be used at the first time,you can use the nextfriendindex for next time which is in the cnf
    uint32_t    get_friendcnt;              //the count of need to get friends
} watch_qq_get_friend_list_req_t;

typedef struct
{
    uint8_t        result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
    uint32_t    update_seq;                 //the update sequence number of friend list
    uint8_t     getover;                    //0 means not completed(use the nextfriendindex for startfriendindex next time),1 means completed
    uint32_t    next_friendindex;           //you can use the nextfriendindex for startfriendindex next time
    uint32_t    user_infocnt;
    lv_ll_t     user_infos_ll;              //user infos,watch_qq_user_info_t
} watch_qq_get_friend_list_cnf_t;

typedef struct
{
    char        openid[64];                    //friend's openid
    uint32_t    lastmsg_time;                //the time of the last readed msg
} watch_qq_c2c_msg_readed_item_t;

typedef struct
{
    char        *cookie;                    //the last cookie which getmsg cnf returned
    uint32_t    c2c_msg_readeditem_cnt;
    watch_qq_c2c_msg_readed_item_t *c2c_msg_readeditem;
} watch_qq_msg_readed_req_t;

typedef struct
{
    uint8_t        result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
    char        *cookie;
} watch_qq_msg_readed_cnf_t;

typedef struct
{
    char        openid[64];                    //friend's openid
} watch_qq_get_friend_info_req_t;

typedef struct
{
    uint8_t        result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
    watch_qq_user_info_t user_info;         //user info
} watch_qq_get_friend_info_cnf_t;

typedef struct
{
    uint32_t        contact_cnt;            //contact count
    uint32_t        contact_time;           //the contact time that you want to query.if you want get all the recent contacts on the server,0 should be used
} watch_qq_get_recent_contact_req_t;

typedef struct
{
    uint8_t        result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
    uint32_t    user_infocnt;
    lv_ll_t     user_infos_ll;              //user infos,watch_qq_user_info_t
} watch_qq_get_recent_contact_cnf_t;

typedef struct
{
    char     qq_id[64];                    //qqid
} watch_qq_find_friend_req_t;

typedef struct
{
    uint8_t        result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
    char        openid[64];                    //friend's openid
} watch_qq_find_friend_cnf_t;

enum{
    WATCH_QQ_SOURCE_FROM_QQ_ID = 0,
    WATCH_QQ_SOURCE_FROM_NICK_NAME,
    WATCH_QQ_SOURCE_FROM_CONDITION_SEARCH,
    WATCH_QQ_SOURCE_FROM_FRIEND_SEARCH,
    WATCH_QQ_SOURCE_FROM_GROUP_SEARCH,
};

typedef struct
{
    char        from_openid[64];            //from openid
    char        to_openid[64];                //to openid
    char        *validation_msg;            //validation message
    uint8_t     sourceid;                   //WATCH_QQ_SOURCE_FROM_QQ_ID or WATCH_QQ_SOURCE_FROM_GROUP_SEARCH
    char        *remark;                    //remark of friend
    uint32_t    myfriend_groupid;           //groupid of friend,default is 0
} watch_qq_add_friend_req_t;

typedef struct
{
    uint8_t        result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
    char        from_openid[64];            //from openid
    char        to_openid[64];                //to openid
    uint8_t        add_result;                    //WATCH_QQ_OK,WATCH_QQ_FAIL
    unsigned char    *errorstring;            //optional,if not exist,ptr is NULL
} watch_qq_add_friend_cnf_t;

enum{
    WATCH_QQ_ACK_TYPE_AGREE = 0,
    WATCH_QQ_ACK_TYPE_REJECT,
};

typedef struct
{
    uint8_t        ack_type;                    //WATCH_QQ_ACK_TYPE_AGREE or WATCH_QQ_ACK_TYPE_REJECT
    char        to_openid[64];                //to openid
    char        *rejectmsg;                 //reject message
    uint32_t    main_sourceid;              //from watch_qq_get_undecided_cnf
    uint8_t     sub_sourceid;               //WATCH_QQ_SOURCE_FROM_QQ_ID or WATCH_QQ_SOURCE_FROM_GROUP_SEARCH
    char        *remark;                    //remark of friend
} watch_qq_ack_add_friend_req_t;

typedef struct
{
    uint8_t        result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
} watch_qq_ack_add_friend_cnf_t;

enum{
    WATCH_QQ_DELETE_TYPE_FROM_OWN_SIDE = 1,
    WATCH_QQ_DELETE_TYPE_FROM_BOTH_SIDE,
};

typedef struct
{
    uint8_t        delete_type;                //WATCH_QQ_DELETE_TYPE_FROM_OWN_SIDE or WATCH_QQ_DELETE_TYPE_FROM_BOTH_SIDE,default is WATCH_QQ_DELETE_TYPE_FROM_BOTH_SIDE
    char        delete_openid[64];            //to openid which deleted
} watch_qq_delete_friend_req_t;

typedef struct
{
    uint8_t        result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
} watch_qq_delete_friend_cnf_t;

enum{
    WATCH_QQ_QUERY_TYPE_PENDING = 0,
    WATCH_QQ_QUERY_TYPE_ALREADY_DECIDED,
    WATCH_QQ_QUERY_TYPE_ALL,
};

typedef struct
{
    uint32_t    validation_seq;                //if cache is exist in local,use the sequence in the cnf last time,otherwise use 0
    uint8_t     query_type;                 //WATCH_QQ_QUERY_TYPE_PENDING ,WATCH_QQ_QUERY_TYPE_ALREADY_DECIDED or WATCH_QQ_QUERY_TYPE_ALL
    char        from_openid[64];            //from openid which to query
    uint32_t    start_index;                //start from 0
    uint32_t    reqnum;                     //the number to get
} watch_qq_get_undecided_req_t;

enum{
    WATCH_QQ_GET_TYPE_SUCCESS = 0,
    WATCH_QQ_GET_TYPE_NO_NEED_UPDATE,
    WATCH_QQ_GET_TYPE_FAIL = 5,
};

typedef struct
{
    char       from_openid[64];             // from openid
    char       friend_openid[64];           // friend openid
    uint8_t    reqtype;                     // 0 means active request,1 means passive request
    uint32_t   timestamp;
    uint32_t   main_sourceid;
    uint8_t    sub_sourceid;                //WATCH_QQ_SOURCE_FROM_QQ_ID or WATCH_QQ_SOURCE_FROM_GROUP_SEARCH
    char       *validation_msg;
}watch_qq_validation_msg_t;

enum{
    WATCH_QQ_UNDECIDED_REQTYPE_RESERVED_0 = 0,
    WATCH_QQ_UNDECIDED_REQTYPE_REQ_ADD_FRIEND,
    WATCH_QQ_UNDECIDED_REQTYPE_AFREE,
    WATCH_QQ_UNDECIDED_REQTYPE_AFREE_AND_ADD,
    WATCH_QQ_UNDECIDED_REQTYPE_FRIEND_AFREE,
    WATCH_QQ_UNDECIDED_REQTYPE_FRIEND_AFREE_AND_ADD,
    WATCH_QQ_UNDECIDED_REQTYPE_REFUSE,
    WATCH_QQ_UNDECIDED_REQTYPE_FRIEND_REFUSE,
    WATCH_QQ_UNDECIDED_REQTYPE_RESERVED_8,
    WATCH_QQ_UNDECIDED_REQTYPE_RESERVED_9,
    WATCH_QQ_UNDECIDED_REQTYPE_RESERVED_10,
    WATCH_QQ_UNDECIDED_REQTYPE_RESERVED_11,
    WATCH_QQ_UNDECIDED_REQTYPE_RESERVED_12,
    WATCH_QQ_UNDECIDED_REQTYPE_WAITING_FRIEND_VERIFY,
};

typedef struct
{
    uint32_t    validation_seq;             //the update of validation sequence
    uint8_t     over;                       //0 means part has been got,1 means all has been got
    uint8_t     totle;                      //the number of get msg,the max number is 100
    lv_ll_t     validation_msgs_ll;            //rpt_msg_all,watch_qq_validation_msg_t
}watch_qq_undecided_msg_successread_t;

typedef struct
{
    uint8_t        result;                        //WATCH_QQ_OK,WATCH_QQ_FAIL
    uint8_t     get_result;                 //WATCH_QQ_GET_TYPE_SUCCESS ,WATCH_QQ_GET_TYPE_NO_NEED_UPDATE or WATCH_QQ_GET_TYPE_FAIL
    watch_qq_undecided_msg_successread_t  msg_successreaded;    // if get_result is WATCH_QQ_GET_TYPE_SUCCESS,this part is valid
} watch_qq_get_undecided_cnf_t;

typedef struct
{
    char       *url;                  //the url of data
} watch_qq_get_data_req_t;

typedef struct
{
    uint8_t        result;                    //WATCH_QQ_OK,WATCH_QQ_FAIL
    char        *url;                   //the url of data
    char        *data;                  //data
    uint32_t    datalen;
} watch_qq_get_data_cnf_t;

extern void watch_qq_get_qr_req(void);

extern void watch_qq_get_qr_cnf(watch_qq_get_qr_cnf_t *get_qrcnf);

extern void watch_qq_query_qr_req(void);

extern void watch_qq_query_qr_cnf(watch_qq_query_qr_cnf_t *query_qrcnf);

extern void watch_qq_register_with_openid_req(watch_qq_register_with_openid_req_t *watch_registerreq);

extern void watch_qq_register_with_openid_cnf(watch_qq_register_with_openid_cnf_t *watch_registercnf);

extern void watch_qq_get_msg_req(watch_qq_get_msg_req_t *get_msgreq);

extern void watch_qq_get_msg_cnf(watch_qq_get_msg_cnf_t *get_msgcnf);

extern void watch_qq_upload_statistics_req(watch_qq_upload_statistics_req_t *upload_statisticsreq);

extern void watch_qq_send_msg_req(watch_qq_send_msg_req_t *send_msgreq);

extern void watch_qq_send_msg_cnf(watch_qq_send_msg_cnf_t *send_msgcnf);

extern void watch_qq_get_friend_msg_req(watch_qq_get_friend_msg_req_t *get_friend_msgreq);

extern void watch_qq_get_friend_msg_cnf(watch_qq_get_friend_msg_cnf_t *get_friend_msgcnf);

extern void watch_qq_get_friend_list_req(watch_qq_get_friend_list_req_t *get_friend_listreq);

extern void watch_qq_get_friend_list_cnf(watch_qq_get_friend_list_cnf_t *get_friend_listcnf);

extern void watch_qq_msg_readed_req(watch_qq_msg_readed_req_t *msg_readedreq);

extern void watch_qq_msg_readed_cnf(watch_qq_msg_readed_cnf_t *msg_readedcnf);

extern void watch_qq_get_friend_info_req(watch_qq_get_friend_info_req_t *get_friend_inforeq);

extern void watch_qq_get_friend_info_cnf(watch_qq_get_friend_info_cnf_t *get_friend_infocnf);

extern void watch_qq_get_recent_contact_req(watch_qq_get_recent_contact_req_t *get_recent_contactreq);

extern void watch_qq_get_recent_contact_cnf(watch_qq_get_recent_contact_cnf_t *get_recent_contactcnf);

extern void watch_qq_find_friend_req(watch_qq_find_friend_req_t *find_friendreq);

extern void watch_qq_find_friend_cnf(watch_qq_find_friend_cnf_t *find_friendcnf);

extern void watch_qq_add_friend_req(watch_qq_add_friend_req_t *add_friendreq);

extern void watch_qq_add_friend_cnf(watch_qq_add_friend_cnf_t *add_friendcnf);

extern void watch_qq_ack_add_friend_req(watch_qq_ack_add_friend_req_t *ack_add_friendreq);

extern void watch_qq_ack_add_friend_cnf(watch_qq_ack_add_friend_cnf_t *ack_add_friendcnf);

extern void watch_qq_delete_friend_req(watch_qq_delete_friend_req_t *delete_friendreq);

extern void watch_qq_delete_friend_cnf(watch_qq_delete_friend_cnf_t *delete_friendcnf);

extern void watch_qq_get_undecided_req(watch_qq_get_undecided_req_t *get_undecidedreq);

extern void watch_qq_get_undecided_cnf(watch_qq_get_undecided_cnf_t *get_undecidedcnf);

extern void watch_qq_get_data_req(watch_qq_get_data_req_t *get_datareq);

extern void watch_qq_get_data_cnf(watch_qq_get_data_cnf_t *get_datacnf);

extern void watch_qq_net_init(void);

extern void watch_qq_net_destroy(void);

#if (WATCH_QQ_NET_TEST == 1)
extern void watch_qq_test_main(void);
#endif

#ifdef __cplusplus
}
#endif /* __cpluscplus */

#endif /* end of _UI_NET_INTERFACE_H_ */
